package com.farm.bpm.form.impl;

import java.util.List;
import java.util.Map.Entry;

import com.farm.bpm.form.Field;
import com.farm.bpm.form.FormParseInter;
import com.farm.core.FarmService;

public class DicListFormParse implements FormParseInter {
	@Override
	public void saveHandle(String fieldType, List<String> parameter,
			String name, Field field) {
		if (!"diclist".equals(fieldType)) {
			return;
		}
	}

	@Override
	public Field parse(String fieldType, List<String> parameter, String name,
			Field field) {
		if (!"diclist".equals(fieldType)) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<select ");
		sb.append(" name='" + field.getName() + "'");
		sb.append(" id='" + field.getId() + "'");
		sb.append(" class='easyui-validatebox'");
		if (field.isRequired()) {
			sb.append(" data-options='required:true'");
		}
		sb.append(" style='width: " + field.getFormWidth() + "px;'");
		sb.append(" val='" + field.getValue() + "'");
		sb.append(" >");
		sb.append("<option value=''>--请选择--</option>");
		if (parameter != null && parameter.size() > 0) {
			List<Entry<String, String>> options = FarmService.getInstance()
					.getParameterService().getDictionaryList(parameter.get(0));
			for (Entry<String, String> node : options) {
				sb.append("<option value='" + node.getKey() + "'>"
						+ node.getValue() + "</option>");
			}
		}
		sb.append("</select>");
		field.setFielsHtmlEdit(sb.toString());
		return field;
	}

	@Override
	public String getfieldTypeDescribes() {
		return "扩展类型:数据字典表单";
	}

	@Override
	public String getfieldTypeFormat() {
		return "name=\"名称#{'type':'diclist','parameter':['dicKey']}\" type='string'";
	}

}
